package com.headwind.controller;

import cn.hutool.core.lang.Dict;
import com.headwind.entity.HwUserBlog;
import com.headwind.service.impl.HwUserBlogServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : Headwind
 * @version : 1.0
 * @date : Created in 2020/8/27
 * @description : 用户博客控制类
 */
@RestController
public class HwMoodController {
    private final HwUserBlogServiceImpl userBlogService;

    @Autowired
    public HwMoodController(HwUserBlogServiceImpl userBlogService) {
        this.userBlogService = userBlogService;
    }

    @PostMapping("/user-blog")
    public Dict save(@RequestBody HwUserBlog userBlog) {
        Boolean save = userBlogService.save(userBlog);
        return Dict.create().set("code", save ? 201 : 500)
                .set("msg", save ? "成功" : "失败").set("data", save ? userBlog : null);
    }

    @DeleteMapping("/user-blog/{id}")
    public Dict delete(@PathVariable Long id) {
        Boolean delete = userBlogService.delete(id);
        return Dict.create().set("code", delete ? 204 : 500)
                .set("msg", delete ? "成功" : "失败");
    }

    @PutMapping("/user-blog/{id}")
    public Dict update(@RequestBody HwUserBlog userBlog, @PathVariable Long id) {
        Boolean update = userBlogService.update(userBlog, id);
        return Dict.create().set("code", update ? 202 : 500)
                .set("msg", update ? "成功" : "失败")
                .set("data", update ? userBlog : null);
    }

    @GetMapping("/user-blog/{id}")
    public Dict getUserBlog(@PathVariable Long id) {
        HwUserBlog user = userBlogService.getUserBlog(id);
        return Dict.create().set("code", 200).set("msg", "成功").set("data", user);
    }

    @GetMapping("/user-blog")
    public Dict getUserBlog(HwUserBlog userBlog) {
        List<HwUserBlog> userList = userBlogService.getUserBlog(userBlog);
        return Dict.create().set("code", 200).set("msg", "成功")
                .set("data", userList);
    }
}
